<template>
  <div class="login">
    <div class="login-head">
      登录
    </div>
    <div class="box">
      <div class="item">
        <p>手机号码</p>
        <input type="text" v-model="username" placeholder="请输入手机号" name="">
      </div>
      <div class="item">
        <p>登录密码</p>
        <input type="password" v-model="password" placeholder="请输入登录密码" name="">
      </div>
      <div class="loginbtn" @click="login()">
        登录
      </div>
      <router-link class="forget goreg" to="/forget">忘记密码？去找回</router-link>
      <router-link class="goreg" to="/reg">还没有账号？去注册</router-link>
    </div>
  </div>
</template>

<script>
import qs from "qs"
export default {
  name: 'App',
  data(){
    return{
      username:'',
      password:''
    }
  },
  methods:{
    login:function(){
      console.log(this.tel,this.pwd)
      this.$http.post("/index.php/index/user/login",qs.stringify({
          mobile:this.username,
          password:this.password
      }))
      .then(res => {
        // console.log(this.$store.state.dlzt)
        // console.log(res.data)
        
        // this.$store.state.dlzt = res.data.code;
        if(res.data.code==-1){
          this.$messagebox('错误了哦~',res.data.message);
        }else if(res.data.code==-2){
          this.$messagebox('错误了哦~',res.data.message);
        }else if(res.data.code==1){
          console.log(res)
          this.$router.push({ path: '/' });
          this.$store.state.dlzt = res.data.code;
          this.$store.state.username=res.data.user_name;
          this.$store.state.userid=res.data.user_id;
          this.$store.state.headimg = res.data.img
          localStorage.name = res.data.user_name
          localStorage.dlzt = res.data.code
          localStorage.userid = res.data.user_id
          localStorage.headimg = res.data.img
          this.$toast('登录成功');
          console.log(localStorage.headimg)
          console.log(res)
        }
        // console.log(this.$store.state.dlzt,this.$store.state.userid)
      })
      .catch(error => {
        alert(网络错误)
      });
    },
      isWeiXin:function() {
        var ua = window.navigator.userAgent.toLowerCase();
        console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
        return true;
        } else {
        return false;
        }
      },
      scjr:function(){

        if(this.isWeiXin()){
            console.log(" 是来自微信内置浏览器");
            var openid = null;
            if(window.location.search){
                var search = window.location.search.substring(1);
                var queryarr = search.split("&");
                var queryObj = {};
                for (var i = 0; i < queryarr.length; i++) {
                  var temp = queryarr[i].split("=");
                  queryObj[temp[0]] = temp[1];
                }
                if(typeof queryObj['openid'] !== "undefinde"){
                  openid = queryObj['openid'];
                  localStorage['openid'] = openid ; 
                }

            }
            else if(typeof localStorage['openid'] !=="undefinde"){
              openid = localStorage['openid'];
            }


            if(openid==null){
              
              this.$http.post("/index.php/user/code/getcode",qs.stringify({
                // mobile:this.tel,
                // password:this.pwd
                // order_number: this.$route.query.ddh

              }))
              .then(res => {
                console.log(res.data.data)
                // window.location.href = res.data
                var urll = res.data.data
                window.location.href = urll
              })
              .catch(error => {
                alert("网络错误")
              });
            }
          }
      }
  },
  components:{
    
  },
  created(){
    this.scjr();
  }
}
</script>

<style scoped lang="less">

.goreg{
  float: right;
  padding-top: .2rem;
  color: #333;
}
.forget{
    float: left;
}
.login-head{
  width: 100%;
  height: .82rem;
  background: #fff;
  text-align: center;
  line-height: .82rem;
  font-size: .24rem;
  color: #333;
  border-top: .14rem solid #eceef3;
  border-bottom: 1px solid #d9dadc;
}
.box{
  padding: .2rem;
  height: auto;
  zoom:1;
  overflow: height; 
  .loginbtn{
    width: 100%;
    height: .7rem;
    margin-top: .4rem;
    text-align: center;
    line-height: .7rem;
    color: #fff;
    font-size: .24rem;
    background: #54b84c;
  }
  .item{
    width: 100%;
    height: .76rem;
    border-bottom: 1px solid #d2d2d2;
    line-height: .76rem;
    p{
      float: left;
      font-size: .24rem;
      color: #333333;
      width: 1.4rem;
      display: block;
      text-align: left;
    }
    input{
      display: block;
      float: left;
      width: 4rem;
      height: 0.76rem;
      border: none;
      outline: none;
      font-size: .24rem;
      box-sizing: border-box;
    }
  }
}
</style>
